package com.wc.AlgoOJ_homework.AlgoOJ_test5.I_问好;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/18 21:15
 * @description http://43.138.190.70:8888/p/1020?tid=65f4f2f02337f3a932b4945c
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    static int[][] a = new int[N][2];
    static int[] b = new int[N];
    static int[] c = new int[N];
    static int t, n;

    // 逆序对数，根据a[i][0]的顺顺序,只有s1 - s2 - e2 - e1, 才能满足碰到，相当于s1 < s2 时候 e2 > e1,逆序了
    public static void main(String[] args) {
        t = sc.nextInt();
        while (t-- > 0) {
            n = sc.nextInt();
            for (int i = 1; i <= n; i++) {
                a[i][0] = sc.nextInt();
                a[i][1] = sc.nextInt();
            }
            Arrays.sort(a, 1, n + 1, Comparator.comparingInt(o -> o[0]));
            for (int i = 1; i <= n; i++) b[i] = a[i][1];
            out.println(mergeSort(1, n));
        }
        out.flush();
    }

    static long mergeSort(int l, int r) {
        if (l >= r) return 0;
        int mid = l + r >> 1;
        long res = mergeSort(l, mid) + mergeSort(mid + 1, r);


        int i = l, j = mid + 1, k = 0;
        while (i <= mid && j <= r) {
            if (b[i] <= b[j]) {
                res += j - 1 - (mid + 1) + 1;
                c[k++] = b[i++];
            } else {
                // 当前的j,  i后面的都大于他
//                res += mid - i + 1;
                c[k++] = b[j++];
            }
        }
        while (i <= mid) {
            res += r - (mid + 1) + 1;
            c[k++] = b[i++];
        }
        while (j <= r) {
            c[k++] = b[j++];
        }
        for (i = l, j = 0; i <= r; i++, j++) {
            b[i] = c[j];
        }
        return res;
    }

}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
